﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 当前控制器使用数据上下文
        /// </summary>
        public readonly HallDbContext _Db;
        public UserController(HallDbContext context)
        {
            _Db = context;
        }
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();

            var resulrData = _Db.EM_User.ToList<EM_User>();
            result.Data = resulrData;
            result.Code = 200;
            result.Author = "3259377702@qq.com";
            return result;
        }
        /// <summary>
        /// 根据ID获取数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{id}/{userName}")]
        public Result Get(Guid Id,string userName)
        {
            Result result = new Result();
            //Where:过滤行
            //select：过滤列
            //tolist():筛选一个范围的数据(集合)
            //FirstOrDefault():筛选第一条符合条件的数据行(对象)
            //<>:确认数据类型(泛型)

            EM_User? resulrData = _Db.EM_User.Where(w => w.Id == Id).FirstOrDefault<EM_User>();
            if (resulrData == null)
            {
                result.Code = 404;
            }
            else
            {
                result.Code = 200;
                result.Data = resulrData;
            }
            return result;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddUser([FromBody] EM_User user)
        {
            Result result = new Result();
            _Db.EM_User.Add(user);
            //SaveChanges:返回值为影响的行数;
            //如果值为0，表示没有人很数据被修改
            //大于0表示有至少1行被修改
            result.Data= _Db.SaveChanges();
            return result;
        }

        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            EM_User? user = _Db.EM_User.Where(w => w.Id == id).SingleOrDefault();
            if(user!=null)
            _Db.EM_User.Remove(user);
            result.Data = _Db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut]
        public Result UpUser([FromBody] EM_User user)
        {
            Result result = new Result();
            _Db.EM_User.Update(user);
            result.Data=_Db.SaveChanges();
            return result;
        }

        //[HttpGet]
        //public Result GetAllUser()
        //{
        //    var UserName = _Db.EM_User.Where(w => w.Name == "张三").Select(s => new
        //    {
        //        s.FristName,
        //        s.LastName
        //    }).FirstOrDefault();

        //    Result result = new Result();
        //    result.Data = UserName;
        //    result.Code = 200;
        //    return result;

        //ToList:查询
        // return _Db.EM_User.ToList();
        //_Db：数据库
        //_Db.EM_User：找到数据库中的EM_User表
        //where select 组装Sql语句
        //where:筛选数据行  决定返回结果有多少行
        //select:筛选数据列 决定返回结果有多少列，但会改变返回的数据类型
        //执行sql语句 toList() FirstOrDefault()
        //顺序 先组装  后执行

    //}
}
}
